package com.accp.controller;


import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.accp.entity.TzAppConnect;
import com.accp.entity.TzUser;
import com.accp.service.ITzAppConnectService;
import com.accp.service.ITzUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.lang.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 辉
 * @since 2019-12-26
 */
@RestController
@RequestMapping("/accp/tzAppConnect")
public class TzAppConnectController {

	@Autowired
	ITzAppConnectService itzAppConnectService;
	@Autowired
	ITzUserService itzUserService;
	
	@GetMapping("/getAppConnect")
	public List<TzAppConnect> getAppConnect(String c,String v) {
		return itzAppConnectService.list(new QueryWrapper<TzAppConnect>().eq(c, v));
	}
	
	@GetMapping("/add")
	public TzAppConnect add(TzAppConnect tzApp,String sex) {
		System.out.println(tzApp);
		
		TzUser tzUser = new TzUser();
		tzUser.setNickName(tzApp.getNickName());
		tzUser.setSex(sex.equals("1")?"M":"F"); //判断性别
		tzUser.setUserLasttime(LocalDateTime.now());
		tzUser.setModifyTime(LocalDateTime.now());
		if(tzApp.getId()>0) { //代表不是新增是修改 
			itzUserService.updateById(tzUser); //添加成功
			itzAppConnectService.updateById(tzApp);
		}else {
			tzApp.setAppId(1);
			//先给User表添加数据再到微信小程序
			//随机生成的id暂时这个没用 暂时用open够了.
			String id = UUID.randomUUID().toString().replaceAll("-", "");
			tzUser.setUserId(id);
			System.out.println(id);
			tzUser.setUserRegtime(LocalDateTime.now()); //注册时间
			tzUser.setUserLasttime(LocalDateTime.now());
						
			tzApp.setUserId(tzUser.getUserId());
			itzUserService.save(tzUser); //添加成功
			itzAppConnectService.save(tzApp);
		}
		System.out.println(tzApp);
		System.out.println(tzUser);
		return tzApp;
	}
}

